<?php

namespace common\models\datas;

use Yii;
use common\components\extenders\MenuManage;
use common\models\datas\Customer;
use common\models\datas\Regions;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "{{%customer_address}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property string $consignee
 * @property integer $province_id
 * @property integer $city_id
 * @property integer $district_id
 * @property string $address
 * @property string $mobile
 * @property string $zip
 * @property integer $created_at
 */
class CustomerAddress extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_address}}';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'province_id', 'city_id', 'district_id', 'created_at'], 'integer'],
            [['consignee'], 'string', 'max' => 255],
            [['address'], 'string', 'max' => 200],
            [['mobile', 'zip'], 'string', 'max' => 100],
            ['mobile', 'match', 'pattern' => '/^1([0-9]{9})/', 'message' => '手机号码格式错误'],
            ['zip','match','pattern' => '/[0-9]{6}/','message' => '邮编格式错误'],
            [['customer_id','consignee','province_id','city_id','district_id','address','mobile','zip'],'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自增id'),
            'customer_id' => Yii::t('common', '用户id'),
            'consignee' => Yii::t('common', '收货人'),
            'province_id' => Yii::t('common', '省id'),
            'city_id' => Yii::t('common', '市id'),
            'district_id' => Yii::t('common', '区id'),
            'address' => Yii::t('common', '详细地址'),
            'mobile' => Yii::t('common', '手机号'),
            'zip' => Yii::t('common', '邮编'),
            'created_at' => Yii::t('common', '创建时间'),
        ];
    }

    public function extraFields()
    {
        return [
            'customer_name'=>function(){
                return isset($this->customer) ? $this->customer->name : '';
            },
            'province_name'=>function(){
                return isset($this->region) ? $this->region->name : '';
            },
            'city_name'=>function(){
                return isset($this->regionCity) ? $this->regionCity->name : '';
            },
            'district_name'=>function(){
                return isset($this->regionDistrict) ? $this->regionDistrict->name : '';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    public function getRegion(){

        return $this->hasOne(Regions::className(),['region_id'=>'province_id']);
    }

    public function getRegionCity(){

        return $this->hasOne(Regions::className(),['region_id'=>'city_id']);
    }

    public function getRegionDistrict(){

        return $this->hasOne(Regions::className(),['region_id'=>'district_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
